<?php
namespace app\index\controller;

use app\BaseController;
use think\facade\Db;
use think\facade\Request;
use app\model\My;
class Domain extends Base
{
 
    public function index()
    {
       $url = 'https://www.urlc.cn/api/url/add';
    $method = 'POST';
   
    // TODO: 设置Token
    $token = '6zFyCruNDuX2YdEXJNFY';
    
    // TODO：设置待检测网址
    $bodys1 = array(
        'url'=>  'http://lanme.cc'
       
    );  
     
$data=$this->apiurl($url,$method,$bodys);
 echo (json_decode($data, true));
   die();
    $login =session('userAccount');    
     if(request()->isPost()){
         
       
            $data=input('post.');

       $sub_domain=$data['domain'];
       $domain_id=substr($data['domains'],0,strrpos($data['domains'],","));
       $domain=substr($data['domains'],strrpos($data['domains'],",")+1);
       
       $doip=$data['doip'];
    $url = 'https://dnsapi.cn/Record.Create';
    $method = 'POST';
   
    // TODO: 设置Token
     $login_token= '301461,c0cde6f09703d45e60cef190d3f5b0df';
    
    // TODO：设置待检测网址
    $data = array('login_token'=>  $login_token ,'domain_id'=> $domain_id,'domain'=> $domain,'record_line_id'=> '10=1','mx'=> '1','record_line'=> '默认','sub_domain'=> $sub_domain,'record_type'=> 'A','value'=> $doip,'format'=>'json','lang'=>'cn');
    $ch = curl_init();
    curl_setopt($ch, CURLOPT_URL, $url);
    curl_setopt($ch, CURLOPT_POST, 1);
    curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 60);
    curl_setopt($ch, CURLOPT_POSTFIELDS , $data);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
    $output = curl_exec($ch);
    $res=json_decode($output, true);

       if($res['status']['code'] == 1){
       $domaindata['uid']=$login['id'];
       $domaindata['domain']=$domain;
       $domaindata['domain_id']=$domain_id;
       $domaindata['name']=$sub_domain;
       $domaindata['ip']=$doip;
       $domaindata['record_id']=$res['record']['id'];
       
        $domaininsert=Db::name('domain')->insertGetId($domaindata);
          if($domaininsert){
			 return alert('添加解析成功！','index',6,3);
			 }else{
				return alert('操作失败！','index',5,3);
			 }
               
      
            }else{
                return alert($res['status']['message'],'index',5,3);
            }
  
  
    curl_close($ch);   
   
     }//post end
      $url = 'https://dnsapi.cn/Domain.List';
    $method = 'POST';
    // TODO: 设置Token
     $login_token= '301461,c0cde6f09703d45e60cef190d3f5b0df';
    
    // TODO：设置待检测网址
    $data = array('login_token'=>  $login_token,'format'=> 'json','lang'=> 'cn','group_id'=> 'all');
    $ch = curl_init();
    curl_setopt($ch, CURLOPT_URL, $url);
    curl_setopt($ch, CURLOPT_POST, 1);
    curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 60);
    curl_setopt($ch, CURLOPT_POSTFIELDS , $data);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
    $output = curl_exec($ch);
  
   $domainarr=json_decode($output, true);
   $domain=$domainarr['domains'];
   $mydomain=Db::name('domain')->where('uid',$login['id'])->paginate([
              'list_rows'=> 20,//每页数量
              'query' => request()->param(),
              ]);
    return view('',[
            'domain'=>$domain,
            'login'=>$login,
            'mydomain'=>$mydomain,
        ]);
    curl_close($ch);   
   
    
    }
public function edit()
   {
   
    $login =session('userAccount');    
     if(request()->isPost()){
         
       
            $data=input('post.');

       $sub_domain=$data['domain'];
       $domain_id=substr($data['domains'],0,strrpos($data['domains'],","));
       $domain=substr($data['domains'],strrpos($data['domains'],",")+1);
       
       $doip=$data['doip'];
    $url = 'https://dnsapi.cn/Record.Create';
    $method = 'POST';
   
    // TODO: 设置Token
     $login_token= '301461,c0cde6f09703d45e60cef190d3f5b0df';
    
    // TODO：设置待检测网址
    $data = array('login_token'=>  $login_token ,'domain_id'=> $domain_id,'domain'=> $domain,'record_line_id'=> '10=1','mx'=> '1','record_line'=> '默认','sub_domain'=> $sub_domain,'record_type'=> 'A','value'=> $doip,'format'=>'json','lang'=>'cn');
    $ch = curl_init();
    curl_setopt($ch, CURLOPT_URL, $url);
    curl_setopt($ch, CURLOPT_POST, 1);
    curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 60);
    curl_setopt($ch, CURLOPT_POSTFIELDS , $data);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
    $output = curl_exec($ch);
    $res=json_decode($output, true);

       if($res['status']['code'] == 1){
       $domaindata['uid']=$login['id'];
       $domaindata['domain']=$domain;
       $domaindata['domain_id']=$domain_id;
       $domaindata['name']=$sub_domain;
       $domaindata['ip']=$doip;
       $domaindata['record_id']=$res['record']['id'];
       
        $domaininsert=Db::name('domain')->insertGetId($domaindata);
          if($domaininsert){
			 return alert('添加解析成功！','index',6,3);
			 }else{
				return alert('操作失败！','index',5,3);
			 }
               
      
            }else{
                return alert($res['status']['message'],'index',5,3);
            }
  
  
    curl_close($ch);   
   
     }//post end
      $url = 'https://dnsapi.cn/Domain.List';
    $method = 'POST';
    // TODO: 设置Token
     $login_token= '301461,c0cde6f09703d45e60cef190d3f5b0df';
    
    // TODO：设置待检测网址
    $data = array('login_token'=>  $login_token,'format'=> 'json','lang'=> 'cn','group_id'=> 'all');
    $ch = curl_init();
    curl_setopt($ch, CURLOPT_URL, $url);
    curl_setopt($ch, CURLOPT_POST, 1);
    curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 60);
    curl_setopt($ch, CURLOPT_POSTFIELDS , $data);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
    $output = curl_exec($ch);
  
   $domainarr=json_decode($output, true);
   $domain=$domainarr['domains'];
   $mydomain=Db::name('domain')->where('uid',$login['id'])->paginate([
              'list_rows'=> 20,//每页数量
              'query' => request()->param(),
              ]);
    return view('',[
            'domain'=>$domain,
            'login'=>$login,
            'mydomain'=>$mydomain,
        ]);
    curl_close($ch);   
   
    
    }

    public function view(){
   
    $name=input('name');
    $login =session('userAccount');  
    $arr=Db::name('domain')->where('name',$name)->where('uid',$login['id'])->find();
    $url = 'https://dnsapi.cn/Record.Info';
    $method = 'POST';
   
    // TODO: 设置Token
     $login_token= '301461,c0cde6f09703d45e60cef190d3f5b0df';
    
    // TODO：设置待检测网址
    $data = array('login_token'=>  $login_token ,'domain'=> $arr['domain'],'record_id'=> $arr['record_id'],'remark'=> '','format'=>'json','lang'=>'cn');
    $ch = curl_init();
    curl_setopt($ch, CURLOPT_URL, $url);
    curl_setopt($ch, CURLOPT_POST, 1);
    curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 60);
    curl_setopt($ch, CURLOPT_POSTFIELDS , $data);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
    $output = curl_exec($ch);
    $res=json_decode($output, true);

       if($res['status']['code'] == 1){
      $domain=$res['domain'];
      $record=$res['record'];
           return view('',[
            'domain'=>$domain,
            'record'=>$record,
        ]);      
      
            }else{
                return alert($res['status']['message'],'index',5,3);
            }
  
  
    curl_close($ch);   
   
  
    }
 

    public function del(){
    $name=input('name');
    $login =session('userAccount');  
    $arr=Db::name('domain')->where('name',$name)->where('uid',$login['id'])->find();
    $url = 'https://dnsapi.cn/Record.Remove';
    $method = 'POST';
   
    // TODO: 设置Token
     $login_token= '301461,c0cde6f09703d45e60cef190d3f5b0df';
    
    // TODO：设置待检测网址
    $data = array('login_token'=>  $login_token ,'domain'=> $arr['domain'],'record_id'=> $arr['record_id'],'format'=>'json','lang'=>'cn');
    $ch = curl_init();
    curl_setopt($ch, CURLOPT_URL, $url);
    curl_setopt($ch, CURLOPT_POST, 1);
    curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 60);
    curl_setopt($ch, CURLOPT_POSTFIELDS , $data);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
    $output = curl_exec($ch);
    $res=json_decode($output, true);

       if($res['status']['code'] == 1){
        $domaindelete=Db::name('domain')->delete($arr['id']);
          if($domaindelete){
              
			 return alert('删除解析记录成功！','index',6,3);
			 }else{
				return alert('删除解析记录失败！','index',5,3);
			 }
               
      
            }else{
                return alert($res['status']['message'],'index',5,3);
            }
  
  
    curl_close($ch);   
   
  
    }

  
}
